package PhamVanHien_XML_DEMO;
import java.io.File;
import java.io.ObjectInputStream.GetField;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Views {
public static void main(String[] args) {
	try{
			ClassLoader objcls = Thread.currentThread().getContextClassLoader();
			URL url = objcls.getResource("PhamVanHien_XML_DEMO/Student.xml");			
			File fXmlFile = new File(url.toURI());
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);		 
			
			doc.getDocumentElement().normalize();	//chuan hoa file document XML
			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());//hien thi ten node goc
			NodeList nList = doc.getElementsByTagName("Student");//lay danh sach phan tu trong file Student
			System.out.println("--------------IMIC--------------");
		    int i = nList.getLength();
		    System.out.println(i);
		    
		    //duyet các phan tu trong list
			for (int temp = 0; temp < nList.getLength(); temp++) {		 
				Node nNode = nList.item(temp);	 
				System.out.println("\nCurrent Element :" + nNode.getNodeName());
		 
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {		 
					Element eElement = (Element) nNode;
					System.out.println("ID: "+eElement.getAttribute("id"));
					System.out.println("RollNo : " + eElement.getElementsByTagName("RollNo").item(0).getTextContent());
					System.out.println("FullName : " + eElement.getElementsByTagName("FullName").item(0).getTextContent());
					System.out.println("Email: " + eElement.getElementsByTagName("Email").item(0).getTextContent());
					System.out.println("Mark: " + eElement.getElementsByTagName("Mark").item(0).getTextContent());
					
				}
			}
	}
		    catch (Exception e) {
			e.printStackTrace();
		    }			
	}
}
